package com.yjhb.android.sbz.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Label;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;

/**
 * 创建者 zx
 * 创建时间 2020/11/18 14:46
 */
public class LabelAdapter extends BaseRecyclerAdapter<LabelAdapter.Holder, Label> {
    private final String TAG = "LabelAdapter";

    private int currSelectedIndex = 0;
    private ItemLabelClickListener itemLabelClickListener;

    public LabelAdapter(Context context) {
        super(context);
    }

    @Override
    protected Holder getHolder(View v) {
        return new Holder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_label;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        if (i == currSelectedIndex) {
            holder.textView.setSelected(true);
            holder.textView.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            holder.textView.setSelected(false);
            holder.textView.setTextColor(context.getResources().getColor(R.color.gray_99));
        }
        holder.textView.setText(data.get(i).getTitle());
        final int index = i;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currSelectedIndex = index;
                if (itemLabelClickListener != null)
                    itemLabelClickListener.onItemLabelClick(data.get(index));
                notifyDataSetChanged();
            }
        });

    }


    public void setItemLabelClickListener(ItemLabelClickListener itemLabelClickListener) {
        this.itemLabelClickListener = itemLabelClickListener;
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView textView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.label_tv);
        }
    }

    public interface ItemLabelClickListener {
        void onItemLabelClick(Label label);
    }

}
